package com.castle.fortress.admin.study.course.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.study.course.entity.StudyCourseChapterCoursewareEntity;
import com.castle.fortress.admin.study.course.dto.StudyCourseChapterCoursewareDto;
import com.castle.fortress.admin.study.course.service.StudyCourseChapterCoursewareService;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.admin.core.annotation.CastleLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;
import javax.servlet.http.HttpServletResponse;
import com.castle.fortress.common.entity.DynamicExcelEntity;
import java.util.List;

/**
 * 章节课件表 控制器
 *
 * @author castle
 * @since 2023-01-31
 */
@Api(tags="章节课件表管理控制器")
@Controller
public class StudyCourseChapterCoursewareController {
    @Autowired
    private StudyCourseChapterCoursewareService studyCourseChapterCoursewareService;

    /**
     * 章节课件表的分页展示
     * @param studyCourseChapterCoursewareDto 章节课件表实体类
     * @param current 当前页
     * @param size  每页记录数
     * @return
     */
    @CastleLog(operLocation="章节课件表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("章节课件表分页展示")
    @GetMapping("/study/course/studyCourseChapterCourseware/page")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseChapterCourseware:pageList")
    public RespBody<IPage<StudyCourseChapterCoursewareDto>> pageStudyCourseChapterCourseware(StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto, @RequestParam(required = false) Integer current, @RequestParam(required = false)Integer size){
        Integer pageIndex= current==null? GlobalConstants.DEFAULT_PAGE_INDEX:current;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<StudyCourseChapterCoursewareDto> page = new Page(pageIndex, pageSize);
        IPage<StudyCourseChapterCoursewareDto> pages = studyCourseChapterCoursewareService.pageStudyCourseChapterCoursewareExtends(page, studyCourseChapterCoursewareDto);

        return RespBody.data(pages);
    }

    /**
     * 章节课件表的列表展示
     * @param studyCourseChapterCoursewareDto 章节课件表实体类
     * @return
     */
    @CastleLog(operLocation="章节课件表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("章节课件表列表展示")
    @GetMapping("/study/course/studyCourseChapterCourseware/list")
    @ResponseBody
    public RespBody<List<StudyCourseChapterCoursewareDto>> listStudyCourseChapterCourseware(StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto){
        List<StudyCourseChapterCoursewareDto> list = studyCourseChapterCoursewareService.listStudyCourseChapterCourseware(studyCourseChapterCoursewareDto);
        return RespBody.data(list);
    }

    /**
     * 章节课件表保存
     * @param studyCourseChapterCoursewareDto 章节课件表实体类
     * @return
     */
    @CastleLog(operLocation="章节课件表",operType = OperationTypeEnum.INSERT)
    @ApiOperation("章节课件表保存")
    @PostMapping("/study/course/studyCourseChapterCourseware/save")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseChapterCourseware:save")
    public RespBody<String> saveStudyCourseChapterCourseware(@RequestBody StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto){
        if(studyCourseChapterCoursewareDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyCourseChapterCoursewareEntity studyCourseChapterCoursewareEntity = ConvertUtil.transformObj(studyCourseChapterCoursewareDto,StudyCourseChapterCoursewareEntity.class);
        if(studyCourseChapterCoursewareService.save(studyCourseChapterCoursewareEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 章节课件表编辑
     * @param studyCourseChapterCoursewareDto 章节课件表实体类
     * @return
     */
    @CastleLog(operLocation="章节课件表",operType = OperationTypeEnum.UPDATE)
    @ApiOperation("章节课件表编辑")
    @PostMapping("/study/course/studyCourseChapterCourseware/edit")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseChapterCourseware:edit")
    public RespBody<String> updateStudyCourseChapterCourseware(@RequestBody StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto){
        if(studyCourseChapterCoursewareDto == null || studyCourseChapterCoursewareDto.getId() == null || studyCourseChapterCoursewareDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyCourseChapterCoursewareEntity studyCourseChapterCoursewareEntity = ConvertUtil.transformObj(studyCourseChapterCoursewareDto,StudyCourseChapterCoursewareEntity.class);
        if(studyCourseChapterCoursewareService.updateById(studyCourseChapterCoursewareEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 章节课件表删除
     * @param id
     * @return
     */
    @CastleLog(operLocation="章节课件表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("章节课件表删除")
    @PostMapping("/study/course/studyCourseChapterCourseware/delete")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseChapterCourseware:delete")
    public RespBody<String> deleteStudyCourseChapterCourseware(@RequestParam Long id){
        if(id == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyCourseChapterCoursewareService.removeById(id)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 章节课件表批量删除
     * @param ids
     * @return
     */
    @CastleLog(operLocation="章节课件表",operType = OperationTypeEnum.DELETE)
    @ApiOperation("章节课件表批量删除")
    @PostMapping("/study/course/studyCourseChapterCourseware/deleteBatch")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseChapterCourseware:deleteBatch")
    public RespBody<String> deleteStudyCourseChapterCoursewareBatch(@RequestBody List<Long> ids){
        if(ids == null || ids.size()<1){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(studyCourseChapterCoursewareService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 章节课件表详情
     * @param id 章节课件表id
     * @return
     */
    @CastleLog(operLocation="章节课件表",operType = OperationTypeEnum.QUERY)
    @ApiOperation("章节课件表详情")
    @GetMapping("/study/course/studyCourseChapterCourseware/info")
    @ResponseBody
    @RequiresPermissions("study:course:studyCourseChapterCourseware:info")
    public RespBody<StudyCourseChapterCoursewareDto> infoStudyCourseChapterCourseware(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        StudyCourseChapterCoursewareDto studyCourseChapterCoursewareDto =  studyCourseChapterCoursewareService.getByIdExtends(id);

        return RespBody.data(studyCourseChapterCoursewareDto);
    }

	/**
     * 动态表头导出 依据展示的字段导出对应报表
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation="章节课件表",operType = OperationTypeEnum.EXPORT)
	@PostMapping("/study/course/studyCourseChapterCourseware/exportDynamic")
	@ApiOperation("动态表头导出，依据展示的字段导出对应报表")
	public void exportDynamic(@RequestBody DynamicExcelEntity<StudyCourseChapterCoursewareDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
		List<StudyCourseChapterCoursewareDto> list = studyCourseChapterCoursewareService.listStudyCourseChapterCourseware(dynamicExcelEntity.getDto());
		//字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
		List<List<Object>> dataList = null;
		/**
        * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
        */
		ExcelUtils.exportDynamic(response,dynamicExcelEntity.getFileName()+".xlsx",null,list,dynamicExcelEntity.getHeaderList(),dataList);
	}


}
